/*
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.model;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.TeamException;
import org.eclipse.ui.IWorkbenchWindow;

/**
  * @version $Revision: 38 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public abstract class PerforceModelElement
{
	// --------------------------------------------------------------------------- Protected Methods

	/** Handles exceptions that occur in Perforce model elements. 
	  * 
	  * @param t exception that should be handled.
	  */
	protected void handle(Throwable t) 
	{
		if (t instanceof InvocationTargetException)
		{
			t = ((InvocationTargetException) t).getTargetException();
		}
		
		IStatus error = null;
		if (t instanceof CoreException) 
		{
			error = ((CoreException) t).getStatus();
		}
		else 
		if (t instanceof TeamException) 
		{
			error = ((TeamException) t).getStatus();
		}
		else 
		{
			error = new Status(Status.ERROR, PerforceUIPlugin.ID, 1, 
				Policy.bind("simpleInternal"), t);                                     //$NON-NLS-1$
		}
	
		Shell shell = new Shell(Display.getDefault());
	
		if (error.getSeverity() == IStatus.INFO)
		{
			MessageDialog.openInformation(shell, Policy.bind("information"),           //$NON-NLS-1$
			                              error.getMessage());
		}
		else
		{
			ErrorDialog.openError(shell, Policy.bind("exception"), null, error);       //$NON-NLS-1$
		}
		
		shell.dispose();

		// Let's log non-team exceptions
		if (!(t instanceof TeamException)) 
		{
			PerforceUIPlugin.log(error);
		}
	}
	
	/** @see IRunnableContext#run(boolean, boolean, IRunnableWithProgress) */ 
	protected void run(boolean fork, boolean canceable, IRunnableWithProgress runnable)
		throws InvocationTargetException, InterruptedException
	{
		IRunnableContext context = null;
		
		IWorkbenchWindow window = PerforceUIPlugin.getActivePage().getWorkbenchWindow();
		if (window instanceof ApplicationWindow)
		{
			context = window;
		}
		else
		{
			context = new ProgressMonitorDialog(window.getShell());
		}
		
		context.run(fork, canceable, runnable);
	}
}
